package com.ls.task.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ls.fw.commons.log.exception.ExceptionsHelper;
import com.ls.fw.data.annotation.FwRequiresPermissions;
import com.ls.fw.data.annotation.bean.AddValidate;
import com.ls.fw.data.annotation.bean.QueryValidate;
import com.ls.fw.data.annotation.bean.SaveValidate;
import com.ls.fw.data.annotation.bean.UpdateValidate;
import com.ls.fw.data.bean.PageBean;
import com.ls.fw.data.bean.ResultStatus;
import com.ls.fw.data.bean.ResultTemplate;
import com.ls.fw.data.rdb.impl.mybatis.plugin.pagehelper.PageHelper;
import com.ls.fw.data.result.ErrorResult;
import com.ls.fw.data.utils.FormUtil;
import com.ls.fw.mvc.springmvc.base.BaseCRUDController;
import com.ls.fw.mvc.springmvc.util.WebUitls;
import com.ls.fw.web.core.servlet.utils.HttpUtils;
import com.ls.task.dal.form.SysTaskFireForm;
import com.ls.task.dal.model.SysTaskFire;
import com.ls.task.dal.service.SysTaskFireService;
import com.ls.task.dal.vo.SysTaskFireVo;
/** 
 * 
 * @author ls
 */
@Controller
@RequestMapping(value = "/sys/task/fire")
public class SysTaskFireController extends BaseCRUDController
{
	public static final String MODEL = "sys_task_fire";
	public static final String MODEL_NAME = "系统任务调用";
   
    @Autowired
	private SysTaskFireService sysTaskFireService;
  
    @RequestMapping("/index") 
    @FwRequiresPermissions(value=""+MODEL+":index:*",name=MODEL_NAME)
    public ModelAndView index(HttpServletRequest req) {
        ModelAndView mavAndView = new ModelAndView("/sys/task/fire/index");
        mavAndView.addAllObjects(HttpUtils.getAllPara(req,false));
        return mavAndView;
    }  
    
    
    @RequestMapping(value = "/"+DELETE)
    @FwRequiresPermissions(value=""+MODEL+":"+DELETE+":*",name=MODEL_NAME)
    public ModelAndView delete(WebRequest webRequest, @RequestParam(required=true ) String id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doDelete(id, webRequest, this.viewPrefix+mapping
		,  new DeleteWorkHandler() {

			@Override
			public ResultTemplate doWork(String[] ids, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				try {
					sysTaskFireService.delete(ids);
					obj.setData(ids);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}

    @RequestMapping(value = "/"+REMOVE)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+","+UPDATE+","+REMOVE+":*",name=MODEL_NAME)
    public ModelAndView remove(WebRequest webRequest, @RequestParam(required=true ) String id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doDelete(id, webRequest, this.viewPrefix+mapping
		, new DeleteWorkHandler() {

			@Override
			public ResultTemplate doWork(String[] ids, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				try {
					sysTaskFireService.remove(ids);
					obj.setData(ids);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}
    @RequestMapping(value = "/"+ADD)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+","+ADD+":*",name=MODEL_NAME)
    public ModelAndView add(WebRequest webRequest, @Validated(value=AddValidate.class) SysTaskFireForm form
                          , final BeanPropertyBindingResult result) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(form, webRequest, this.viewPrefix+mapping
             , new SaveWorkHandler<SysTaskFireForm>() {

			@Override
			public ResultTemplate doWork(SysTaskFireForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				if(result.hasErrors()){
					obj.setData(ErrorResult.from(result));
					obj.setStatus(ResultStatus.NOT_ACCEPTABLE);
				}else{
					try {
						SysTaskFire model = new SysTaskFire();
				    	FormUtil.copyProperties(form, model);
				    	sysTaskFireService.save(forModel(model));
						obj.setData(model);
						obj.setStatus(ResultStatus.OK);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
						obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
					}
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/to"+ADD)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+","+ADD+":*",name=MODEL_NAME)
    public ModelAndView toAdd(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doTo("", webRequest, this.viewPrefix+mapping
             ,  new ToWorkHandler() {

			@Override
			public ResultTemplate doWork(String id, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				obj.setPara(WebUitls.getAllPara(webRequest));
				//TODO:调用service
				try {
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/"+UPDATE)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+","+UPDATE+":*",name=MODEL_NAME)
    public ModelAndView update(WebRequest webRequest, @Validated(value=UpdateValidate.class) SysTaskFireForm form
	                         , final BeanPropertyBindingResult result) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(form, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskFireForm>() {

			@Override
			public ResultTemplate doWork(SysTaskFireForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				if(result.hasErrors()){
					obj.setData(ErrorResult.from(result));
					obj.setStatus(ResultStatus.NOT_ACCEPTABLE);
				}else{
					try {
						SysTaskFire model = new SysTaskFire();
				    	FormUtil.copyProperties(form, model);
						sysTaskFireService.update(forModel(model));
						obj.setData(model);
						obj.setStatus(ResultStatus.OK);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
						obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
					}
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/to"+DETAIL)
    @FwRequiresPermissions(value=""+MODEL+":"+DETAIL+":*",name=MODEL_NAME)
    public ModelAndView toDetail(WebRequest webRequest, @RequestParam(required=true ) String id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doTo(id, webRequest, this.viewPrefix+mapping
             ,  new ToWorkHandler() {

			@Override
			public ResultTemplate doWork(String id, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				obj.setPara(WebUitls.getAllPara(webRequest));
				//TODO:调用service
				try {
					SysTaskFire data = sysTaskFireService.find(Long.parseLong(id));
					obj.setData(data);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}

    @RequestMapping(value = "/to"+UPDATE)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+","+UPDATE+":*",name=MODEL_NAME)
    public ModelAndView toUpdate(WebRequest webRequest, @RequestParam(required=true ) String id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doTo(id, webRequest, this.viewPrefix+mapping
             ,  new ToWorkHandler() {

			@Override
			public ResultTemplate doWork(String id, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				obj.setPara(WebUitls.getAllPara(webRequest));
				//TODO:调用service
				try {
					SysTaskFire data = sysTaskFireService.find(Long.parseLong(id));
					obj.setData(data);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/"+SAVE)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+":*",name=MODEL_NAME)
    public ModelAndView save(WebRequest webRequest, @Validated(value=SaveValidate.class) SysTaskFireForm form
	                       , final BeanPropertyBindingResult result) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(form, webRequest, this.viewPrefix+mapping
		     ,  new SaveWorkHandler<SysTaskFireForm>() {

			@Override
			public ResultTemplate doWork(SysTaskFireForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				if(result.hasErrors()){
					obj.setData(ErrorResult.from(result));
					obj.setStatus(ResultStatus.NOT_ACCEPTABLE);
				}else{
					try {
						SysTaskFire model = new SysTaskFire();
				    	FormUtil.copyProperties(form, model);
				    	sysTaskFireService.persist(forModel(model));
						obj.setData(model);
						obj.setStatus(ResultStatus.OK);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
						obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
					}
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/to"+EDIT)
    @FwRequiresPermissions(value=""+MODEL+":"+EDIT+":*",name=MODEL_NAME)
    public ModelAndView toEdit(WebRequest webRequest, @RequestParam(required=false ) String id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doTo(id, webRequest, this.viewPrefix+mapping
		     ,  new ToWorkHandler() {

			@Override
			public ResultTemplate doWork(String id, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				obj.setPara(WebUitls.getAllPara(webRequest));
				//TODO:调用service
				try {
					if(StringUtils.isNotBlank(id)){
						SysTaskFire data = sysTaskFireService.find(Long.parseLong(id));
						obj.setData(data);
					}
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
	}


    @RequestMapping(value = "/"+QUERY)
    @FwRequiresPermissions(value=""+MODEL+":"+QUERY+":*",name=MODEL_NAME)
    public ModelAndView query(WebRequest webRequest, boolean noPage, final String _style 
	                        , @RequestParam(value="page",required=false,defaultValue="1") Integer page
	                        , @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize
	                        , @Validated(value=QueryValidate.class) SysTaskFireVo q
						    , final BeanPropertyBindingResult result) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doQuery(noPage, page, pageSize, q, webRequest
		     , this.viewPrefix+mapping
		     , new QueryWorkHandler<SysTaskFireVo>() {

			@Override
			public ResultTemplate doWork(boolean noPage, Integer page,
					Integer pageSize, SysTaskFireVo q, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				//TODO:调用service
				if(result.hasErrors()){
					obj.setData(ErrorResult.from(result));
					obj.setStatus(ResultStatus.NOT_ACCEPTABLE);
				}else{
					try {
						 if(!noPage){
			                if(page==null){
			                    page = PageBean.PAGE;
			                }
			                if(pageSize==null){
			                    pageSize = PageBean.PAGE_SIZE;
			                }
			                PageHelper.startPage(page,pageSize); 
			            }
						List list = sysTaskFireService.query(q);
						obj.setData(toData(obj,_style, list));
						obj.setStatus(ResultStatus.OK);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
						obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
					}
				}
				return obj;
			}
		});
	}
}
